from django.shortcuts import render

# Create your views here.
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination

from apiView import models
from . import serializer
from rest_framework import generics


# 实现查询全部和创建功能
class TagView(ListAPIView, CreateAPIView):
    queryset = models.Tag.objects.all()
    serializer_class = serializer.TagSerializers

    def get_serializer_class(self):
        '''
        重写父类中的方法，可以自己定制使用哪个序列化器等
        :return:
        '''
        if self.request.method == 'GET':
            return serializer.TagSerializers
        else:
            return serializer.OtherTagSerializers

    def perform_create(self, serializer):
        '''
        重写CreateAPIView类中的储存方法
        :param serializer:
        :return:
        '''
        serializer.save(author=1)

# 实现根据主键id单个查询和更新，删除功能
# class PostTagView(RetrieveAPIView, UpdateAPIView, DestroyAPIView):
class PostTagView(generics.RetrieveUpdateDestroyAPIView):
    queryset = models.Tag.objects.all()
    serializer_class = serializer.TagSerializers
